import java.util.*;

/* Finding minimum distance between two elements in an array.
 */
class MinDist {

	// Driver program to test the logic.
	public static void main(String[] args) {
		List<Integer> arrayData = new ArrayList<Integer>();
		int[] data = {0,1,2,7,8,1,1,3,4,9,2,2,3,1,6,7,4,4,9,0,5,3};
		for(Integer x: data)
			arrayData.add(x);

		Integer min =  minDist(arrayData, 7, 9);

		if(min == null) {
			System.out.println("Either or both elements not found in the given array.");
		} else {
			System.out.println("Minimum distance: " + min);
		}
	}

	private static Integer minDist(List<Integer> inData, int x, int y) {
		int j = -1; // stores position of x in array
		int k = -1; // stores position of y in array

		int jj = -1; // stores position of x' in array i.e. repeated x
		int kk = -1; // stores position of y' in array i.e. repeated y

		for(int z = 0; z < inData.size(); ++z){
			if( inData.get(z) == x) {
				jj = z;
				if (j == -1 || Math.abs(j - k) >= Math.abs(jj - k))
					j = z;
			} 
			if(inData.get(z) == y) {
				kk = z;
				if (k == -1 || Math.abs(j - k) >= Math.abs(j - kk))
					k = z;
			}
		}

		if (j == -1 || k == -1) return null;

		return (Math.abs(j - k));
	}




}
